package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Contact;
import cn.wolfcode.crm.query.ContactQueryObject;
import cn.wolfcode.crm.service.IContactService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contact")
public class ContactController {

    @Autowired
    private IContactService contactService;

    @RequestMapping("list")
    @ResponseBody
    public List<Contact> list() {
        return contactService.selectAll();
    }

    @RequestMapping("view")
    @RequiresPermissions(value = {"contact:view","联系人界面"},logical = Logical.OR)
    public String query(ContactQueryObject qo) {
        return "contact";
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"contact:saveOrUpdate","学校联系人新增/更新"},logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Contact contact) {
        JSONResult result = new JSONResult();
        try {
            contactService.saveOrUpdate(contact);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"contact:saveOrUpdate","删除学校联系人"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            contactService.delete(id);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult contact(ContactQueryObject qo) {
        return contactService.query(qo);
    }
}
